{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "aec2ac77",
   "metadata": {},
   "source": [
    "---------\n",
    "* 日期：2022-10-12 | week08 \n",
    "* 课程：Python\n",
    "* 记录人：杨静雯~\n",
    "---------\n",
    "\n",
    "# 本周学习内容\n",
    "\n",
    ">1.有序的数据结构\n",
    ">>1.字符串的基本概念（回顾）\n",
    ">>> 1.\n",
    ">>> 2.\n",
    ">>> 3.\n",
    ">>2.字符串的性质和方法\n",
    ">2.有序的数据结构-列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac1bf921",
   "metadata": {},
   "source": [
    "## 1.字符串的性质和方法\n",
    "\n",
    "* 字符串的性质\n",
    "> 1.字符串的有序性\n",
    "> 2.字符串是有长度的\n",
    ">> 1.使用len()检查字符串的长度\n",
    "\n",
    "* 字符串的基础方法（运算法则）\n",
    "> 1.字符串的取值：str[index]\n",
    "> 2.字符串的切片：用:号 str[start:stop],stop取不到\n",
    "> 3.字符的拼接：用+号\n",
    "> 4.in 检查成员是否存在\n",
    "* 字符串的一般方法（function/methods 函数/方法）\n",
    "> 1.isdigit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "460c9482",
   "metadata": {},
   "outputs": [],
   "source": [
    "text =\"思想者团队需要人来\"  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e4f0a406",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'来'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[8]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9a5a5357",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'要'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0187d94e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'来'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "ed851463",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "string index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[1;32mIn [18]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mtext\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m100\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[1;31mIndexError\u001b[0m: string index out of range"
     ]
    }
   ],
   "source": [
    "text[100]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f658670c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "98d3d481",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'思想者'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[0]+text[1]+text[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e266cd6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'思想者'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[0:3] # 等价于 text[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "8dda0db7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'思想者'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "8c1a910c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'要人'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-3:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "61eb7e87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'要人来'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-3:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "69d6d920",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'思想者团队需要人来思想者团队需要人来'"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2*text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe6120ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "ctrl+f 可以查询该页面 是否存在相应单词"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "011b4bf2",
   "metadata": {},
   "outputs": [],
   "source": [
    "* in 检查成员对象\n",
    "* count()检查字符串出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "51241f3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"广州\"in text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "67af9a52",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"思想者\"in text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "2e13c448",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text.count(\"思想者\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "517cf643",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2*text.count(\"思想者\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd90c8bd",
   "metadata": {},
   "source": [
    "* isdigit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "0e1524be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19\n",
      "19\n"
     ]
    }
   ],
   "source": [
    "print(19)\n",
    "print(\"19\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "c21c110a",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_1 = 19\n",
    "age_2 =\"19\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2dc2925",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ce98a11",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb4740e4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ce4edee",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e55d7dc4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03d84668",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7410a9d0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "3dce67ab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19 <class 'int'>\n",
      "19 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(age_1,type(age_1))\n",
    "print(age_2,type(age_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "9032157e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "38\n"
     ]
    }
   ],
   "source": [
    "# 如果字符串是数值，可以用int转换成真正数值\n",
    "if age_2.isdigit():\n",
    "    print(age_1 + int(age_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "107ed299",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for +: 'int' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [47]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mage_1\u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43mage_2\u001b[49m\n",
      "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'"
     ]
    }
   ],
   "source": [
    "age_1 + age_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "951a50fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"LUCY\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "12218850",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "38"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_1 + int(age_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "5dc029e9",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'name' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [52]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mint\u001b[39m(\u001b[43mname\u001b[49m)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'name' is not defined"
     ]
    }
   ],
   "source": [
    "int(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "564cad25",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (3100413433.py, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [55]\u001b[1;36m\u001b[0m\n\u001b[1;33m    > 1.列表的基本概念和性质\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "## 2.有序的数据结构-列表（list)\n",
    "\n",
    "* 1.列表的基本概念和性质\n",
    "> 1.\n",
    "* 2.列表一般方法（如何学习）\n",
    "> 1.使用.Tab键查看列表的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "e957e7d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'8'"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text=\"158948662\"\n",
    "text[5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a88717c",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 2.1 列表的性质\n",
    "* 和字符串的运算法则完全一致\n",
    "* 注意：字符串是对字符进行操作，列表是对items元素进行操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "a825be8c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表的形式\n",
    "squares = [1,4,9,16,25] # 列表里面可以放数值\n",
    "squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "899c3309",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "d66c51eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "4 in squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "1fee7138",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jack', 'alex', 'jalynn', 'jane']"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = [\"jack\",\"alex\",\"jalynn\",\"jane\"] # 列表里面放字符串\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "e0354183",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jack', 18, '刘石珍', 19]"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info = [\"jack\",18,\"刘石珍\",19]\n",
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "b6237a9d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares.count(9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "e28eac00",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25, 'jack', 'alex', 'jalynn', 'jane']"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares + names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "3fa82e11",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25, 1, 4, 9, 16, 25]"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2*squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "22ed4fe0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['alex', 'jalynn']"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "0a89e314",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['alex', 'jalynn']"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names[-3:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "1906ef43",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jalynn', 'jane']"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names[-2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "710b3b1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 取出\"Jack\"，\"jakynn\"用切片的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "45139642",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jack', 'jalynn']"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names[:3:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68181c24",
   "metadata": {},
   "source": [
    "* 2.列表一般方法（如何学习）\n",
    "> 1.增加\n",
    "> 2.删除\n",
    "> 3.改\n",
    "> 4.查：in\n",
    "* 列表的学习方法（步骤）\n",
    "> 1.使用.Tab键查看列表的方法\n",
    "> 2.使用help（查看方法的使用规则）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e56bdd57",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"jack\",\"alex\",\"jalynn\",\"jane\"] \n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "79434fb6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.append)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "77245a07",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.append(\"liushizhen\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "bb8dd4ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jack',\n",
       " 'alex',\n",
       " 'jalynn',\n",
       " 'jane',\n",
       " 'liushizhen',\n",
       " 'liushizhen',\n",
       " 'liushizhen',\n",
       " 'liushizhen',\n",
       " 'liushizhen']"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49e7f295",
   "metadata": {},
   "source": [
    "# 课后作业\n",
    "* 列表的方法尝试 所有的方法\n",
    "* 总结列表的方法心得（最好是用表格的形式）\n",
    "|列表的方法| description|使用方法|备注|\n",
    "|-----------|-----------|--------|-----|\n",
    "|append|append object to the end of the list = 在末尾增加一个值|list.append(object）|注意：添加在末尾！|\n",
    "|paragraph|text|text|text|\n",
    "|count|统计某个元素在列表中出现的次数|list.count(object）|\n",
    "|extend|在列表末尾一次性追加另一个序列中的多个用新列表扩展原来的列表）|list.extend(seq）|\n",
    "|index|从列表中找出某个值第一个匹配项的索引位置|list.index(obj)|\n",
    "|insert|在指定位置添加元素|list.insert(index, obj)|\n",
    "|pop|删除指定位置的元素|list.pop([index=-1])|\n",
    "|remove|移除列表中某个值的第一个匹配项|list.remove(obj)|\n",
    "|reverse|颠倒列表的顺序|list.reverse()|\n",
    "|sort|对原列表进行排序|list.sort(cmp=None, key=None, reverse=False)|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "111acc89",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
